import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import { getDoctors, insertDoctors, updateDoctors, deleteDoctors, batchSaveDoctorPhoto, batchSaveDoctors } from '@/api/doctor'
import store from '@/store'
import { IDoctorModel } from '@/api/types'

export interface IDeptState {

}

@Module({ dynamic: true, store, name: 'doctors' })
class Doctors extends VuexModule implements IDeptState {

    @Action
    public async getDoctors() {
        return new Promise((r: any, j: any) => {
            getDoctors({}).then((value: any) => {
                console.error("Doctors:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateDoctors(obj: any) {
        return new Promise((r: any, j: any) => {
            updateDoctors(obj).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j({})
            })
        })
    }

    @Action
    public async insertDoctors(obj: any) {
        return new Promise((r: any, j: any) => {
            insertDoctors(obj).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j({})
            })
        })
    }

    @Action
    public async deleteDoctors(staffId: string) {
        return new Promise((r: any, j: any) => {
            deleteDoctors({ staffId: staffId }).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

    @Action
    public async batchSaveDoctorPhoto() {
        return new Promise((r: any, j: any) => {
            batchSaveDoctorPhoto().then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

    @Action
    public async batchSaveDoctors(arr: Array<IDoctorModel>) {
        return new Promise((r: any, j: any) => {
            batchSaveDoctors({ doctors: arr }).then((value: any) => {
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j(0)
            })
        })
    }

}

export const DoctorsModule = getModule(Doctors)
